/**
 * Created by yangxianming on 2018/4/20.
 */
//单词表
var currentPhraseList = [];
var confusionPhraseList = [];//临时存放乱序单词
var tempPhraseList = [];
var wrongPhraseList = [];
var orgPhraseList = [];
var startPhrase = 0;

//
//  api接口
//
function apiGetPhrase(){
    //step修改一组的数量
    var step = 15;
    var from = Number(selectedText(document.getElementById("phrase_sele1"))) * step - (step);
    var to = Number(selectedText(document.getElementById("phrase_sele2"))) * step;
    var body = {"from":from,"to":to};
    wrongPhraseList = [];

    var data = {
        "action":"getPhrase",
        "body":body
    };

    var xmlHttp1 = new XMLHttpRequest();
    xmlHttp1.onreadystatechange = function(){
        if (xmlHttp1.readyState==4){
            if(xmlHttp1.status==200){
                var r = JSON.parse(xmlHttp1.response);
                if (r["apiStatus"] == "0") {
                    alert(r["errorMsg"])
                }
                if (r["apiStatus"] == "1"){
                    startPhrase = 0;
                    currentPhraseList = r["result"];
                    orgPhraseList = [];
                    orgPhraseList = orgPhraseList.concat(r["result"]);
                    confusionPhrase();
                    drawPhrase(currentPhraseList[0]);
                    document.getElementById("phrase_s1").setAttribute("class", "collapse");
                }
            }
        }
    };
    xmlHttp1.open("POST", "/english/getWords", true);
    xmlHttp1.send(JSON.stringify(data));
}

function recordMistakePhrase(){
    var updateList = [];
    var strList = "";
    for(var i = 0; i < wrongPhraseList.length; i++){
        updateList.push(wrongPhraseList[i][0]);
    }
    strList = updateList.join(",");
    var par = "words=" + strList;
    var url = "http://192.168.137.1:1985/english/updateFGWords?" + par;
    alert(strList);
}

//页面绘制

//显示当前单词
function drawPhrase(data){
	var bdLink = "https://fanyi.baidu.com/?aldtype=16047#en/zh/";
    document.getElementById("phrase_chinese").style.visibility="hidden";
    var wordData = data[0];
    //var wordData = "occupy oneself (in doing sth. / with sth.)";
    var wordSize = wordData.length;
    var chineseData = data[1];
    var word = document.getElementById("phrase_word");
    var chinese = document.getElementById("phrase_chinese");
	word.onclick = function(){window.open(bdLink+wordData);};
    if(wordSize > 30){
        word.style.fontSize = 50+'px';
    }else if(wordSize > 20){
        word.style.fontSize = 75+'px';
    }else{
        word.style.fontSize = 100+'px';
    }
    word.innerText = "";
    word.innerText = wordData;
    chinese.innerText = chineseData;
}

//轮询单词
function drawNextPhrase(){
    startPhrase += 1;
    //没有直接退出
    if(0 == currentPhraseList.length){
        apiGetPhrase();
        //alert("没有单词加载");
        return;
    }
    //判断到尾部
    if(startPhrase >= currentPhraseList.length){
        if(wrongPhraseList.length != 0){
            recordMistakePhrase();
            alert("把记错的再背一边吧");
            tempPhraseList = currentPhraseList;
            currentPhraseList = wrongPhraseList;
            wrongPhraseList = [];
        }else{
            alert("结束了,可以再来一边");
            if(tempPhraseList.length != 0){
                currentPhraseList = orgPhraseList;
                wrongPhraseList = [];
            }
            confusionPhrase();
        }
        startPhrase = 0;
        drawPhrase(currentPhraseList[0]);
        console.log(currentPhraseList);
        console.log(wrongPhraseList);
        return;
    }
    drawPhrase(currentPhraseList[startPhrase]);
}
function drawPhraseWrongMark(){
    if((wrongPhraseList.length == 0) || (wrongPhraseList[wrongPhraseList.length-1][0] != currentPhraseList[startPhrase][0])){
        wrongPhraseList.push(currentPhraseList[startPhrase]);
    }
    currentPhraseList[startPhrase][2] = Number(currentPhraseList[startPhrase][2]) + 1;
}

function drawPhraseButton2(){
    var b2 = document.getElementById("phrase_b2");
    var name = b2.getAttribute("name");
    b2.innerText = "";
    if(name == "1"){
        b2.setAttribute("name", "2");
        b2.innerText = "混乱";
    }else{
        b2.setAttribute("name", "1");
        b2.innerText = "秩序";
    }
}

//
//打开页面后初始化phrase界面
function initializePhrase(){
    //初始化phrase界面
    //设置中文解释点击显示
    document.getElementById("phrase_chineseParent").onclick = function(){document.getElementById("phrase_chinese").style.visibility="visible";};
    //加载给词按钮
    document.getElementById("phrase_b1").onclick = function(){apiGetPhrase()};
    document.getElementById("phrase_b2").onclick = function(){drawPhraseButton2()};
    //加载下个一个和错误按钮
    document.getElementById("phrase_next").onclick = function(){drawNextPhrase()};
    document.getElementById("phrase_error").onclick = function(){drawPhraseWrongMark()};

    //加载 组 下拉数据 每天一共50组，
    var phrase_sele1 = document.getElementById("phrase_sele1");
    var phrase_sele2 = document.getElementById("phrase_sele2");
    for (var x = 1; x<=50; x++){
        phrase_sele1.options.add(new Option(x, x));
        phrase_sele2.options.add(new Option(x, x));
    }
}

//根据 秩序/混乱 按钮确定单词列表是否要乱序
function confusionPhrase(){
    var name = document.getElementById("phrase_b2").getAttribute("name");
    if(name == "1"){
        currentPhraseList = orgPhraseList;
    }
    if(name == "2"){
        var le = currentPhraseList.length;
        for(var s = 0; s < le; s++){
            var currentLength = currentPhraseList.length;
            var rIndex = Math.floor(Math.random()*currentLength);
            confusionPhraseList.push(currentPhraseList[rIndex]);
            currentPhraseList.splice(rIndex, 1);
        }
        currentPhraseList = confusionPhraseList;
        confusionPhraseList = [];
    }
}